<script setup>
import { ref,onMounted } from 'vue'
import { queryLevelAll,saveLevel,queryLevelById,updateLevel,deleteLevel } from '../../api/level'
import { ElMessage } from 'element-plus'

//数据预加载
onMounted(()=>{
  query()
})

//定义表单数据
const form = ref ({name:'',status:''})

//点击重置的点击事件
const clear = () => {form.value = {name:'',status:''},query()}
//table表格数据定义
const tableData = ref([])

//定义对话框数据
const title = ref(null)
const dialogShow = ref(false)
const dialogData = ref({name:'',lplanId:'',fee:'',status:'',description:''})
//添加的点击事件
const addLevel = () =>{ 
  title.value = '添加护理等级'
  dialogShow.value = true
  //清空表单数据
  dialogData.value = {name:'',lplanId:'',fee:'',status:'',description:''}
}
//回显查询的方法
const edit = async (id) =>{ 
  title.value = '编辑护理等级'
  dialogShow.value = true
  const result = await queryLevelById(id)
  dialogData.value = result.data
}
//保存数据的点击事件
const saveLevelProject = async () =>{ 
  let result = null
  if(dialogData.value.id){
    result = await updateLevel(dialogData.value)
  }else{
    result = await saveLevel(dialogData.value)
  }
  if(result.code){
    query()
    dialogShow.value = false
    ElMessage.success("操作成功")
  }else{
    ElMessage.error("操作失败")
  }
}
//删除数据的点击事件
const deleteById = async (id) =>{
  const result = await deleteLevel(id)
  if(result.code){
    query()
    ElMessage.success("删除成功")
  }else{
    ElMessage.error("删除失败")
  }
}

//条件分页查询方法
const query = async () =>{
  const result = await queryLevelAll(currentPage.value,pageSize.value,form.value.name,form.value.status)
  tableData.value = result.data.rows
  total.value = result.data.total
}

//定义分页数据
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
//分页方法
const handleSizeChange = (val) => {
  currentPage.value = 1
  query()
}
const handleCurrentChange = (val) => {
  query()
}
</script>

<template>
  <!-- 表单 -->
<div>
  <el-form :inline="true" :model="form" class="demo-form-inline">
    <el-form-item label="等级名称">
      <el-input v-model="form.name" placeholder="请输入等级名称" clearable />
    </el-form-item>
    <el-form-item label="状态">
      <el-select style="width: 200px;" v-model="form.status" placeholder="请选择" clearable>
        <el-option label="启用" value="1" />
        <el-option label="禁用" value="0" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="query">查询</el-button>
      <el-button type="default" @click="clear">重置</el-button>
    </el-form-item>
  </el-form>
</div>
<!-- 新增按钮 -->
 <div>
  <el-button type="primary" @click="addLevel"> + 新增护理等级</el-button>
 </div>
<!-- 表格数据展示 -->
<div>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column type="index" label="序号"  align="center"/>
    <el-table-column prop="name" label="等级名称"  align="center"/>
    <el-table-column prop="lplanId" label="护理计划" align="center"/>
    <el-table-column prop="fee" label="护理费用" align="center"/>
    <el-table-column label="状态" align="center">
      <template #default="scope">
        <el-tag align="center" v-if="scope.row.status == 1" type="success">启用</el-tag>
        <el-tag align="center" v-else type="danger">禁用</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" align="center"/>
    <el-table-column label="操作" align="center">
      <template #default="scope">
        <el-button type="primary" size="small" @click="edit(scope.row.id)">修改</el-button>
        <el-button type="danger" size="small" @click="deleteById(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</div>
<!-- 分页 -->
<div>
  <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      :size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"/>
</div>

<!-- 弹出框 -->
 <el-dialog v-model="dialogShow" :title="title" width="500" align-center>
    <el-form :model="dialogData">
      <el-form-item label="等级名称" :label-width="formLabelWidth">
        <el-input v-model="dialogData.name" placeholder="请输入等级名称" />
      </el-form-item>
      <el-form-item label="护理计划" :label-width="formLabelWidth">
        <el-input v-model="dialogData.lplanId" placeholder="请选择" />
      </el-form-item>
      <el-form-item label="费用" :label-width="formLabelWidth">
        <el-input v-model="dialogData.fee" placeholder="请输入费用" />
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="dialogData.status">
          <el-radio border value="1">启用</el-radio>
          <el-radio border value="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="等级说明" :label-width="formLabelWidth">
        <el-input v-model="dialogData.description" placeholder="请输入" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogShow = false" type="default">取消</el-button>
        <el-button type="primary" @click="saveLevelProject">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
</style>